<?php
namespace app\small\controller;
use app\common\base\_Db;
use think\Controller;
use think\Db;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/12
 * Time: 11:01
 */
class Info extends Controller{
    /*
     * 通知公告
     */
    public function Info(){
        $result = Db::table('info_manage')
            ->field('information_id,information_content')
            ->where('information_status = 2 ')
            ->order('oper_date desc')
            ->select();
        return json_encode($result);
    }
    /*
     * 广告
     */
    public function Advertise(){
        $result = Db::table('advertisement_info')
            ->field('advertisement_id,url')
            ->where('advertisement_status = 2 ')
            ->order('oper_date desc')
            ->select();
        foreach ( $result as $key => $value){
            $result[$key]['url'] = HttpPath .$value ['url'];
        };
        return json_encode($result);
    }
    /*
     * 线路
     */
    public function Line(){
        $result = Db::table('line')
            ->field(
                [
                    'a.line_id'           => 'Id',          //主键id
                    'a.line_from'         => 'From',        //起点
                    'a.line_destination'  => 'Destination', //终点
                    'a.oper_date'         => 'Date',        //操作日期
                    'a.price'             => 'Price',       //票价
                ]
            )
            ->alias('a')
            ->join('staff c','a.oper_id = c.staff_id','LEFT')
            ->order('a.oper_date desc')
            ->select();
        return json_encode($result);
    }
    /*
     * 乘车记录
     */
    public function Order(){
        $uid = input('uid');
        $date = input('date');
        //$date = '2018-11-01|2018-11-02';
        $iden = input('iden');
        $res= Db::table('user')->field('user_id as uid,iden,openid')->where('user_id = '.$uid)->find();
        if ($iden == 1){
            $where = 'openid = "'.$res['openid'].'"';
            $this->GetData($where,$date);
        }elseif ($iden == 2){
            //机场职工
            $table_name='staff';
            $field = array('staff_id'=>'uid','regist'=>'regist','openid'=>'openid');
            if (empty($res)){
                //uid非user表id
                $res_staff = _Db::GetData($table_name,$field,'staff_id = '.$uid);
                $where =  'openid = "'.$res_staff['openid'].'"';
            }else{
                $where =  'openid = "'.$res['openid'].'"';
            }
            $this->GetData($where,$date);

        }elseif ($iden == 3){
            //驻场职工
            $table_name='external_staff';
            $field = array('id'=>'uid','regist'=>'regist','openid'=>'openid');
            if (empty($res)){
                $res_exter = _Db::GetData($table_name,$field,'id = '.$uid);
                $where =  'openid = "'.$res_exter['openid'].'"';
            }else{
                $where =  'openid = "'.$res['openid'].'"';
            }
            $this->GetData($where,$date);
        }else{
            $data ['result'] = '';
            $data ['sum'] = 0;
            return json_encode($data,true);
        }
    }
    public function GetData($where,$date){
        $array_data_range = explode('|', $date);
        if(!empty($date)){
            if (is_array($array_data_range) && $array_data_range[0] !== '' && $array_data_range[1] !== '') {
                $where .= " and pay_time between '" . $array_data_range[0] . " 00:00:00'" . " and '" . $array_data_range[1] . " 23:59:59'";
            }
        }
        //用户表订单
        $res_user = Db::table('order_info')
            ->field('a.order_id as Id,a.pay_money as Money,a.pay_time as Time,e.type_name as TypeName,b.line_from as LineFrom,b.line_destination as LineDes')
            ->alias('a')
            ->join('sys_public_type e','a.trade_state = e.type_id and e.type_classify = "订单状态"','LEFT')
            ->join('line b','a.line_id = b.line_id','LEFT')
            ->where($where)
            ->order('pay_time desc')
            ->select();
        //echo Db::table('')->getLastSql();
        $res_count=Db::table('order_info')
            ->field('sum(pay_money)sum')
            ->where($where . ' and trade_state =2')
            ->find();
        if (empty($res_count['sum'])){
            $res_count['sum'] = 0;
        }
        $data ['result'] = $res_user;
        $data ['sum'] = $res_count['sum'];
        exit(json_encode($data,true)) ;
    }
}




